﻿package core.ui
{
    import core.enum.*;
    import core.ui.controls.*;
    import core.ui.options.*;
    import core.ui.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;

    public class Optionbar extends Sprite
    {
        var ed:Tuyitu;
        var lt:Literal;
        var ll:Literal;
        var bg:Bitmap;
        var icon:Bitmap;
        var cmenu:Menu;
        private var holder:Sprite;
        private var brs:Brushes;
        private var grd:Gradients;
        public var minmax:Sprite;
        public var selected:Object;
        public var type:TypeOptions;
        public var clone:CloneOptions;
        public var brush:BrushOptions;
        public var pencil:PencilOptions;
        public var fill:FillOptions;
        public var wand:WandOptions;
        public var blur:BlurOptions;
        public var sharpen:SharpenOptions;
        public var smudge:SmudgeOptions;
        public var lasso:LassoOptions;
        public var burn:BurnOptions;
        public var dodge:DodgeOptions;
        public var sponge:SpongeOptions;
        public var bloat:BloatOptions;
        public var drawing:DrawingOptions;
        public var pinch:PinchOptions;
        public var eraser:EraserOptions;
        public var redeye:RedEyeReductionOptions;
        public var gradient:GradientOptions;
        public var marquee:MarqueeOptions;
        public var colorreplace:ColorReplaceOptions;
        public var spotheal:SpotHealOptions;
        public var crop:CropOptions;

        public function Optionbar(param1:Tuyitu)
        {
            this.ed = param1;
            var _loc_2:* = new DropShadowFilter(2, 45, 0, 0.5, 3, 3, 1, 2);
            this.bg = new Bitmap(new Optionbarbg());
            this.bg.filters = [_loc_2];
            addChild(this.bg);
            var _loc_3:* = new BarSplit();
            _loc_3.x = 10;
            _loc_3.y = 1;
            addChild(_loc_3);
            this.icon = new Bitmap();
            this.icon.x = 22;
            this.icon.y = 4;
            addChild(this.icon);
            var _loc_4:* = new BarSplit();
            _loc_4.x = 50;
            _loc_4.y = 1;
            addChild(_loc_4);
            this.holder = new Sprite();
            this.holder.x = 50;
            this.holder.y = 1;
            addChild(this.holder);
            this.lt = new Literal("rm_no_additional_options");
            this.lt.x = 60;
            this.lt.y = 3;
            addChild(this.lt);
            this.ll = new Literal("rm_rasterize_before_edit");
            this.ll.x = 60;
            this.ll.y = 3;
            addChild(this.ll);
            this.minmax = new Sprite();
            this.minmax.y = 5;
            this.minmax.visible = false;
            var _loc_5:* = new IconButton(param1, "rm_close", new WinCloseOn());
            _loc_5.addEventListener(MouseEvent.CLICK, this.CloseMeClick, false, 1, true);
            _loc_5.x = 40;
            this.minmax.addChild(_loc_5);
            var _loc_6:* = new IconButton(param1, "rm_min", new WinMaxOn());
            _loc_6.addEventListener(MouseEvent.CLICK, this.MinMeClick, false, 1, true);
            _loc_6.x = 20;
            this.minmax.addChild(_loc_6);
            var _loc_7:* = new IconButton(param1, "rm_images", new WinListOn());
            _loc_7.addEventListener(MouseEvent.CLICK, this.MenuMouseClick, false, 1, true);
            this.minmax.addChild(_loc_7);
            addChild(this.minmax);
            return;
        }// end function

        private function MenuMouseClick(... args) : void
        {
            if (!AppState.Modald)
            {
                this.cmenu.visible = true;
                this.cmenu.x = this.minmax.x - 184;
                stage.addEventListener(MouseEvent.MOUSE_DOWN, this.StageMouseDown, false, 0, true);
            }
            return;
        }// end function

        private function CloseMeClick(... args) : void
        {
            if (!AppState.Modald)
            {
                this.ed.cm.ConfirmClose();
            }
            return;
        }// end function

        private function MinMeClick(... args) : void
        {
            if (!AppState.Modald)
            {
                this.ed.Minimize();
            }
            return;
        }// end function

        private function StageMouseDown(event:MouseEvent) : void
        {
            this.cmenu.visible = false;
            stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.StageMouseDown);
            return;
        }// end function

        public function UpdateCanvasList() : void
        {
            var _loc_1:int = 0;
            var _loc_2:Canvas = null;
            if (this.cmenu == null)
            {
                this.cmenu = new Menu();
                this.cmenu.visible = false;
                this.cmenu.y = 18;
                addChild(this.cmenu);
            }
            this.cmenu.Clear();
            while (_loc_1 < this.ed.area.numChildren)
            {
                
                _loc_2 = this.ed.area.getChildAt(_loc_1) as Canvas;
                this.cmenu.AddItem(_loc_2.ForceActivate, _loc_2.document.GetName().substr(0, 35));
                _loc_1++;
            }
            return;
        }// end function

        public function get brushes() : Brushes
        {
            if (this.brs == null)
            {
                this.brs = new Brushes(this.ed);
                addChild(this.brs);
            }
            return this.brs;
        }// end function

        public function get gradients() : Gradients
        {
            if (this.grd == null)
            {
                this.grd = new Gradients(this.ed);
                addChild(this.grd);
            }
            return this.grd;
        }// end function

        public function SetTool(rm_close:ToolType) : void
        {
            if (this.holder.numChildren > 0)
            {
                this.holder.removeChildAt(0);
            }
            this.lt.visible = false;
            this.ll.visible = rm_close.attitude && rm_close.attitude.locked;
            switch(rm_close)
            {
                case ToolType.Move:
                {
                    this.icon.bitmapData = new MoveIcon();
                    this.lt.visible = true;
                    break;
                }
                case ToolType.Marquee:
                {
                    if (this.marquee == null)
                    {
                        this.marquee = new MarqueeOptions(this.ed);
                    }
                    this.holder.addChild(this.marquee);
                    this.icon.bitmapData = new MarqueeIcon();
                    break;
                }
                case ToolType.Wand:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.wand == null)
                        {
                            this.wand = new WandOptions(this.ed);
                        }
                        this.holder.addChild(this.wand);
                    }
                    this.icon.bitmapData = new WandIcon();
                    break;
                }
                case ToolType.Lasso:
                {
                    if (this.lasso == null)
                    {
                        this.lasso = new LassoOptions(this.ed);
                    }
                    this.holder.addChild(this.lasso);
                    this.icon.bitmapData = new LassoIcon();
                    break;
                }
                case ToolType.Crop:
                {
                    if (this.crop == null)
                    {
                        this.crop = new CropOptions(this.ed);
                    }
                    this.holder.addChild(this.crop);
                    this.icon.bitmapData = new CropIcon();
                    break;
                }
                case ToolType.Hand:
                {
                    this.icon.bitmapData = new HandIcon();
                    this.lt.visible = true;
                    break;
                }
                case ToolType.Picker:
                {
                    this.lt.visible = true;
                    this.icon.bitmapData = new PickerIcon();
                    break;
                }
                case ToolType.Type:
                {
                    if (this.type == null)
                    {
                        this.type = new TypeOptions(this.ed);
                    }
                    this.holder.addChild(this.type);
                    this.icon.bitmapData = new TypeIcon();
                    break;
                }
                case ToolType.Drawing:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.drawing == null)
                        {
                            this.drawing = new DrawingOptions(this.ed);
                        }
                        this.holder.addChild(this.drawing);
                    }
                    this.icon.bitmapData = new DrawingIcon();
                    break;
                }
                case ToolType.Zoom:
                {
                    this.lt.visible = true;
                    this.icon.bitmapData = new ZoomIcon();
                    break;
                }
                case ToolType.Fill:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.fill == null)
                        {
                            this.fill = new FillOptions(this.ed);
                        }
                        this.holder.addChild(this.fill);
                    }
                    this.icon.bitmapData = new FillIcon();
                    break;
                }
                case ToolType.Gradient:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.gradient == null)
                        {
                            this.gradient = new GradientOptions(this.ed);
                        }
                        this.holder.addChild(this.gradient);
                    }
                    this.icon.bitmapData = new GradientIcon();
                    break;
                }
                case ToolType.Brush:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.brush == null)
                        {
                            this.brush = new BrushOptions(this.ed);
                        }
                        this.holder.addChild(this.brush);
                    }
                    this.icon.bitmapData = new BrushIcon();
                    break;
                }
                case ToolType.Pencil:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.pencil == null)
                        {
                            this.pencil = new PencilOptions(this.ed);
                        }
                        this.holder.addChild(this.pencil);
                    }
                    this.icon.bitmapData = new PencilIcon();
                    break;
                }
                case ToolType.Eraser:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.eraser == null)
                        {
                            this.eraser = new EraserOptions(this.ed);
                        }
                        this.holder.addChild(this.eraser);
                    }
                    this.icon.bitmapData = new EraserIcon();
                    break;
                }
                case ToolType.ColorReplace:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.colorreplace == null)
                        {
                            this.colorreplace = new ColorReplaceOptions(this.ed);
                        }
                        this.holder.addChild(this.colorreplace);
                    }
                    this.icon.bitmapData = new ColorReplaceIcon();
                    break;
                }
                case ToolType.Smudge:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.smudge == null)
                        {
                            this.smudge = new SmudgeOptions(this.ed);
                        }
                        this.holder.addChild(this.smudge);
                    }
                    this.icon.bitmapData = new SmudgeIcon();
                    break;
                }
                case ToolType.Clone:
                case ToolType.CloneMarker:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.clone == null)
                        {
                            this.clone = new CloneOptions(this.ed);
                        }
                        this.holder.addChild(this.clone);
                    }
                    this.icon.bitmapData = new CloneIcon();
                    break;
                }
                case ToolType.SpotHeal:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.spotheal == null)
                        {
                            this.spotheal = new SpotHealOptions(this.ed);
                        }
                        this.holder.addChild(this.spotheal);
                    }
                    this.icon.bitmapData = new SpotHealIcon();
                    break;
                }
                case ToolType.RedEyeReduction:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.redeye == null)
                        {
                            this.redeye = new RedEyeReductionOptions(this.ed);
                        }
                        this.holder.addChild(this.redeye);
                    }
                    this.icon.bitmapData = new RedEyeIcon();
                    break;
                }
                case ToolType.Bloat:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.bloat == null)
                        {
                            this.bloat = new BloatOptions(this.ed);
                        }
                        this.holder.addChild(this.bloat);
                    }
                    this.icon.bitmapData = new BloatIcon();
                    break;
                }
                case ToolType.Pinch:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.pinch == null)
                        {
                            this.pinch = new PinchOptions(this.ed);
                        }
                        this.holder.addChild(this.pinch);
                    }
                    this.icon.bitmapData = new PinchIcon();
                    break;
                }
                case ToolType.Dodge:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.dodge == null)
                        {
                            this.dodge = new DodgeOptions(this.ed);
                        }
                        this.holder.addChild(this.dodge);
                    }
                    this.icon.bitmapData = new DodgeIcon();
                    break;
                }
                case ToolType.Burn:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.burn == null)
                        {
                            this.burn = new BurnOptions(this.ed);
                        }
                        this.holder.addChild(this.burn);
                    }
                    this.icon.bitmapData = new BurnIcon();
                    break;
                }
                case ToolType.Sponge:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.sponge == null)
                        {
                            this.sponge = new SpongeOptions(this.ed);
                        }
                        this.holder.addChild(this.sponge);
                    }
                    this.icon.bitmapData = new SpongeIcon();
                    break;
                }
                case ToolType.Blur:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.blur == null)
                        {
                            this.blur = new BlurOptions(this.ed);
                        }
                        this.holder.addChild(this.blur);
                    }
                    this.icon.bitmapData = new BlurIcon();
                    break;
                }
                case ToolType.Sharpen:
                {
                    if (!rm_close.attitude.locked)
                    {
                        if (this.sharpen == null)
                        {
                            this.sharpen = new SharpenOptions(this.ed);
                        }
                        this.holder.addChild(this.sharpen);
                    }
                    this.icon.bitmapData = new SharpenIcon();
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (this.holder.numChildren > 0)
            {
                this.selected = this.holder.getChildAt(0) as IToolOptions;
            }
            return;
        }// end function

        public function Resize() : void
        {
            this.bg.width = stage.stageWidth;
            this.minmax.x = stage.stageWidth - 65;
            return;
        }// end function

        public function get current() : Object
        {
            return this.selected && this.selected.options ? (this.selected.options) : (null);
        }// end function

    }
}
